package com.ebring.shareEducation.controller;


import com.ebring.shareEducation.bean.Menu;
import com.ebring.shareEducation.bean.RoleMenu;
import com.ebring.shareEducation.bean.admin.MenuAll;
import com.ebring.shareEducation.bean.response.MenuOperation;
import com.ebring.shareEducation.service.MenuService;
import com.ebring.shareEducation.utils.RspMsg;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "菜单对象管理" ,tags = {"菜单对象管理接口"})
@CrossOrigin(origins = "http://wangliang.vipgz1.idcfengye.com", maxAge = 3600)
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单列表
     *
     * @return
     */
    @PostMapping("/menu/list")
    @ApiOperation(value="查询所有菜单列表", notes="查询所有菜单列表")
    public List<Menu> select() {
        return menuService.selectMenu();
    }

    /**
     * 根据一级查询菜单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/menu/Grade")
    @ApiOperation(value="根据一级查询菜单", notes="根据一级查询菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
    })
    public RspMsg<List<MenuAll>> selectOne(@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return menuService.selectMenuGrade();
    }

    /**
     * 查询下级菜单
     *
     * @param menuId 机构编号
     * @return 查询结果集合
     */
    @PostMapping("/menu/getLower")
    @ApiOperation(value="查询下级菜单", notes="查询下级菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "一级菜单编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
    })
    public RspMsg<MenuAll> getLower(String menuId,
                                    @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return menuService.getLower(menuId);
    }

    /**
     *查询当前id的所有父级
     * @param menuId
     * @return
     */
    @PostMapping("/menu/getParentAll")
    @ApiOperation(value="查询当前id的所有父级", notes="查询当前id的所有父级")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
    })
    public RspMsg<List<MenuAll>> getParentAll(String menuId){
        return menuService.getParentAll(menuId);
    }

    /**
     * 查询上级菜单
     *
     * @param menuId
     *            机构编号
     * @return 查询结果
     */
    @PostMapping("/menu/getParent")
    @ApiOperation(value="查询上级菜单", notes="查询上级菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
    })
    public RspMsg<MenuAll> getParent(String menuId) {
        return menuService.getParent(menuId);
    }

    /**
     * 模糊查询机构
     *
     * @param menuId
     *            机构编号
     * @param menuName
     *            机构名称
     * @return 查询结果集合
     */
    @PostMapping("/menu/select")
    @ApiOperation(value="模糊查询菜单", notes="模糊查询菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "menuName", value = "菜单名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "parentId", value = "父级菜单编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "menuGrade", value = "菜单级别", required = true, dataType = "String"),

            @ApiImplicitParam(paramType="query",name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType="query",name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
    })
    public RspMsg<List<MenuAll>> select(String menuId, String menuName,String parentId,
                                        @RequestParam(name = "menuGrade", required = false, defaultValue = "1")String menuGrade,
                                        @RequestParam(name = "pageNum", required = false, defaultValue = "1")Integer pageNum,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "5")Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        return menuService.select(menuId, menuName,menuGrade,parentId);
    }

    /**
     * 修改菜单
     */
    @PostMapping("/menu/update")
    @ApiOperation(value="修改菜单", notes="修改菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "Menu", value = "菜单对象", required = true, dataType = "Menu"),
    })
    public RspMsg<Menu> updateRole(Menu menu) {
        return menuService.updateMenu(menu);
    }

    /**
     * 根据菜单ID查找菜单
     *
     * @param menuId 菜单id
     * @return
     */
    @PostMapping("/menu/selectById")
    @ApiOperation(value="根据菜单ID、角色ID查找菜单", notes="根据菜单ID、角色ID查找菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "roleId", value = "角色编号", required = true, dataType = "String")
    })
    public RspMsg<RoleMenu> selectUserByUserId(String menuId, String roleId) {
        return menuService.selectMenuByMenuId(menuId,roleId);
    }

   /* @PostMapping("/menu/selectById")
    public RspMsg<Menu> selectUserByUserId(String menuId) {
        return menuService.selectMenuByMenuId(menuId);
    }*/

    /**
     * 根据菜单ID删除菜单
     *
     * @param menuId 菜单id
     * @return
     */

    @GetMapping("/menu/deleteById")
    @ApiOperation(value="根据菜单ID删除菜单", notes="根据菜单ID删除菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
    })
    public RspMsg<Menu> deleteById(String menuId) {
        return menuService.deleteById(menuId);
    }

    /**
     * 新增菜单
     *
     * @param menu 菜单对象
     * @return
     */

    @PostMapping("/menu/insertMenu")
    @ApiOperation(value="新增菜单", notes="新增菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "Menu", value = "菜单对象", required = true, dataType = "Menu"),
    })
    public RspMsg<Menu> insertMenu(Menu menu) {
        return menuService.insertMenu(menu);
    }

    /**
     * 查询所有界面控件
     * @return
     */
    @GetMapping("/menu/selectOperation")
    @ApiOperation(value="查询所有界面控件", notes="查询所有界面控件")
    public RspMsg<MenuOperation> selectOperation(){
        return menuService.selectOperation();
    }

    /**
     *根据菜单id查询界面控件
     * @return
     */
    @PostMapping("/menu/selectOperationById")
    @ApiOperation(value="根据菜单id查询界面控件", notes="根据菜单id查询界面控件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menuId", value = "菜单编号", required = true, dataType = "String"),
    })
    public RspMsg<Menu> selectOperationById(String menuId){

        return menuService.selectOperationById(menuId);
    }

    /**
     * 查询用户角色下面的菜单权限
     *
     * @param roleId
     * @return
     */
    @PostMapping("/userMenu/select")
    @ApiOperation(value="查询用户角色下面的菜单权限", notes="查询用户角色下面的菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "roleId", value = "角色编号", required = true, dataType = "String"),
    })
    public List<Menu> selectMenu(String roleId){
        return menuService.selectUMenu(roleId);
    }


    @PostMapping("/menu/selectMenuByRoleId")
    @ApiOperation(value="查询用户角色下面的菜单权限按钮权限", notes="查询用户角色下面的菜单权限按钮权限")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "roleId", value = "角色编号", required = true, dataType = "String"),
    })
    public List<HashMap> selectMenuByRoleId(String roleId) {
        return menuService.selectMenuByRoleId(roleId);
    }


    /**
     * 根据用户ID查找菜单
     *
     * @param
     * @return
     */
    @PostMapping("/menu/updateMenus")
    @ApiOperation(value="给角色添加相应的菜单", notes="给角色添加相应的菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "menus", value = "角色编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "roleId", value = "角色编号", required = true, dataType = "String"),
            @ApiImplicitParam(value = "一级菜单id_&二级菜单1id_按钮1id_按钮2id_&二级菜单2id_按钮1id_&二级菜单3id_按钮1id_按钮2id_按钮3id_&二级菜单4id_按钮1id_按钮2id_&二级菜单5id_按钮1id&")
    })
    public List<HashMap> updateMenus(String menus,String roleId) {
        return menuService.updateMenus(menus,roleId);
    }
}
